package org.summerframework.core.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import lombok.extern.slf4j.Slf4j;
import org.summerframework.core.xml.WaitInitializedBean.Dependence;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * <p>xml配置文件解析器</p>
 * 功能：读取并解析xml文件中存在的Bean对象
 *
 * @author sumou
 * @date 2025/01/26
 */
@Slf4j
public class SummerXmlParser {

    /**
     * 文档构建工厂
     */
    private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    public WaitInitializedBean parseXml(InputStream stream) {
        Document document = null;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(stream);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
        return this.parseXmlDocument(document);
    }

    public WaitInitializedBean parseXml(File file) {
        Document document = null;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(file);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
        return this.parseXmlDocument(document);
    }

    /**
     * 解析xml文件文档
     *
     * @param document xml文件中解析出来的文档
     * @return 等待被初始化的Bean的集合类
     */
    private WaitInitializedBean parseXmlDocument(Document document) {
        // 判断文档是否为空
        if (document == null) {
            return null;
        }
        // 初始化
        WaitInitializedBean bean = new WaitInitializedBean();
        // 获取Bean对象的节点信息
        HashMap<String, String> map = new HashMap<>();
        NodeList nodeList = document.getElementsByTagName("bean");
        // 遍历Bean节点
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                String id = element.getAttribute("id");
                String clazz = element.getAttribute("class");
                map.put(id, clazz);
            }
        }
        bean.setBeanMap(map);
        // 遍历Property的节点信息
        List<Dependence> dependences = new ArrayList<>();
        NodeList propertyNodes = document.getElementsByTagName("property");
        for (int j = 0; j < propertyNodes.getLength(); j++) {
            Dependence dependence = new Dependence();
            Node propertyNode = propertyNodes.item(j);
            if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
                Element propertyElement = (Element) propertyNode;
                String propertyName = propertyElement.getAttribute("name");
                String ref = propertyElement.getAttribute("ref");
                dependence.setName(propertyName);
                dependence.setRef(ref);
            }
            Node parentNode = propertyNode.getParentNode();
            if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element parentElement = (Element) parentNode;
                String id = parentElement.getAttribute("id");
                dependence.setBeanId(id);
            }
            dependences.add(dependence);
        }
        bean.setDepList(dependences);
        return bean;
    }

}
